package com.category.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 跨域配置
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();

        // 允许前端开发端口访问
        config.addAllowedOrigin("http://localhost:3200");
        config.addAllowedOrigin("http://127.0.0.1:3200");
        
        // 允许生产环境域名（HTTP 和 HTTPS）
        config.addAllowedOrigin("http://152.136.176.21:3200");
        config.addAllowedOrigin("https://152.136.176.21:3200");
        config.addAllowedOrigin("https://iseeyou1.icu:3200");
        config.addAllowedOrigin("https://www.iseeyou1.icu:3200");

        // 允许所有请求头
        config.addAllowedHeader("*");
        
        // 允许所有HTTP方法
        config.addAllowedMethod("*");
        
        // 允许发送Cookie
        config.setAllowCredentials(true);
        
        // 预检请求的有效期（秒）
        config.setMaxAge(3600L);
        
        // 注册CORS配置
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        
        return new CorsFilter(source);
    }
}
